import { createRouter, createWebHistory } from 'vue-router'

import LayoutContainer from '@/views/layout/LayOutContainer.vue'
import LoginPage from '@/views/login/LoginPage.vue'
import ArticleManage from '@/views/article/ArticleManage.vue'
import ArticleChannel from '@/views/article/ArticleChannel.vue'
import UserProfile from '@/views/user/UserProfile.vue'
import UserAvatar from '@/views/user/UserAvatar.vue'
import UserPassword from '@/views/user/UserPassword.vue'
import { useUserStore } from '@/stores/user.js'
import Dashboard from '@/views/dashboard/Dashboard.vue'
const routes = [
  {
    path: '/',
    component: LayoutContainer,
    redirect: '/article/manage',
    children: [
      { path: '/article/manage', component: ArticleManage },
      { path: '/article/channel', component: ArticleChannel },
      { path: '/user/profile', component: UserProfile },
      { path: '/user/avatar', component: UserAvatar },
      { path: '/user/password', component: UserPassword },
      { path: '/dashboard', component: Dashboard }
    ]
  },
  { path: '/login', component: LoginPage }
]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  const token = userStore.token
  if ( !token && to.path !== '/login' ) return '/login'
})

export default router
